import { SearchAction } from '@/types/store'
import { getLocalHistories } from '@/utils/storage'
import { Result } from '@/types/data'
type SearchState = {
  suggestions: string[]
  histories: string[]
  searchResult: Result
}
const init: SearchState = {
  suggestions: [],
  histories: getLocalHistories(),
  searchResult: {} as Result,
}
const profile = (state = init, action: SearchAction): SearchState => {
  if (action.type === 'search/suggestion') {
    return {
      ...state,
      suggestions: action.payload,
    }
  }
  if (action.type === 'search/saveHistory') {
    return {
      ...state,
      histories: action.payload,
    }
  }
  if (action.type === 'search/searchResult') {
    return {
      ...state,
      searchResult: action.payload,
    }
  }
  return state
}

export default profile
